const shopsModel = require('../model/shopsModel')
const goodsModel = require('../model/goodsModel')
const servicesModel = require('../model/servicesModel')
module.exports.findAll = async function (searchObj, page) {
    let { current, size } = page;
    //获取数据总数
    let total = await shopsModel.countDocuments(searchObj);
    //根据总数/每页显示数得到总页数
    let totalPage = Math.ceil(total / size);
    //limit是每页显示数，skip通过（当前页-1）*每页显示得到
    let rows = await shopsModel
        .find(searchObj)
        .limit(parseInt(size))
        .skip((current - 1) * size)
        .populate({ path: "goodsId", model: goodsModel })
        .populate("adminId")
        .populate({ path: "servicesId", model: servicesModel })
    return {
        current, size, total, totalPage, rows
    };
    // return await shopsModel.find().populate({ path: "goodsId", model: goodsModel }).populate({ path: "servicesId", model: servicesModel })
}

//增加
module.exports.insert = async function (cinemas) {
    return await shopsModel.create(cinemas);
}
//根据ID删除
module.exports.delete = async function (id) {
    return await shopsModel.deleteOne({ _id: id });
}
//根据ID查询
module.exports.findById = async function (id) {
    return await shopsModel.findById(id)
        .populate({ path: "goodsId", model: goodsModel })
        .populate("adminId")
        .populate({ path: "servicesId", model: servicesModel });
}
//修改
module.exports.update = async function (id, cinemas) {
    return await shopsModel.updateOne({ _id: id }, cinemas);
}